<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class export_service{
	private $CI;
	function __construct() { 
		$this->CI = &get_instance();		
		$this->CI->load->library('PHPExcel/PHPExcel');
	}

	/**  
	* 获取园区账户
	* @access public 
	* @param  $param cond参数
	* @param  $del
	*/  
	public function export($file_name,$title,$value) {
        $excel = new PHPExcel();
        //公共样式
        $excel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(11); 
        $excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setWrapText(true);
        $excel->getActiveSheet()->getDefaultColumnDimension()->setWidth(23);
        $excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $excel->getActiveSheet()->getRowDimension('1')->setRowHeight(20);

        $x_line=0;
        $y_line=1;

		//标题
        // $title=array('合同类型',' 客户名','负责人','负责人电话','合同起始','合同终止','创建人','创建时间','未结束状态');
        foreach ($title as $k => $v) {
        	$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($x_line).$y_line,$v);
        	$excel->getActiveSheet()->getStyle(PHPExcel_Cell::stringFromColumnIndex($x_line).$y_line)->getFont()->setBold(true);   
        	$x_line++;
        }

        $x_line=0;
        $y_line=2;
        //正式数据
        foreach ($value as $k => $v) {        	
        	foreach ($v as $k1 => $v1) {        		
        		$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($x_line).$y_line,$v1);	        			
        			$x_line++;
        	}
        	$x_line=0;
        	$y_line++;
        }
        $write = new PHPExcel_Writer_Excel2007($excel); 
       	$director = "upload/export/" . date('Ymd', time()) . "/";
        if (!file_exists($director)) {
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($director, 0777);
        }        
		$file_name =$file_name.date('Ymd').'.xlsx';       	
       	$write->save($director.iconv('utf-8', 'gb2312', $file_name));
		exit(retJson('返回地址',true,$director.$file_name));
		return $admin;
	}


	
	/**  
	* 导出excel表
	* @access public 
	* @param  $arraylist   导出数据列
	* @param  $name      导出excel名称
	* @param  $tableth     表头名称  *
  * @param  $t   表头从第n行开始导出 
  * @param  $i  表数据从第n行开始导出
  * @param  $title   表标题,不传默认为空
	* @autor  xy
	*/  
	public function export_excel($arraylist,$name,$tableth,$t,$i,$title='') {
		$excel = new PHPExcel();
	   $excel->getActiveSheet()->getDefaultColumnDimension()->setWidth(15);

	   //获取标题
	  $excel->getActiveSheet()->mergeCells(PHPExcel_Cell::stringFromColumnIndex(0).'1:'.PHPExcel_Cell::stringFromColumnIndex(6).'1');
		$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex(0).'1',$title);
		$excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		$excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(16);
		$excel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
				//表头
				$table_th = explode(',',$tableth);
				for ($m=0; $m < count($table_th); $m++) { 
			$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($m).($t),$table_th[$m]);
			}

			//表数据
			foreach ($arraylist as $k => $v) {
				$v = array_values($v);	//重新排列数组
				for($column = 0;$column < count($v);$column++)	{
					$excel->getActiveSheet()->setCellValue(PHPExcel_Cell::stringFromColumnIndex($column).$i,$v[$column]);	  		
				}
				$i ++;
			}

				$excel->getActiveSheet()->getStyle('A1:'.PHPExcel_Cell::stringFromColumnIndex(9).($i-1))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
			$excel->getActiveSheet()->getStyle('A1:'.PHPExcel_Cell::stringFromColumnIndex(0).($i-1))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

		$write = new PHPExcel_Writer_Excel2007($excel); 
		$director = "upload/export/" . date('Ymd', time()) . "/";
		if (!file_exists($director)) {
			//检查是否有该文件夹，如果没有就创建，并给予最高权限
			mkdir($director, 0777);
		}     
		$file_name =$name.'报表.xlsx';         
		$write->save($director.iconv('utf-8', 'gb2312', $file_name));

		return array('director'=>$director,'file_name'=>$file_name);
}



  /**  
  * excel表导入
  * @access public 
  * @param  $pathArray   导入文件地址(必传)
  * @param  $begin  导入开始列  默认A列开始导入(非必传)
  * @param  $end   导入结束列  (非必传)
  * @param  $row  第N行开始导入,默认第二行开始导入(非必传)
  * @autor  xy
  */ 
  public function importexcel($pathArray,$end='',$begin='A',$row=2){

	try {
	  $inputFileType = IOFactory::identify($pathArray['path']);
	  $objReader = IOFactory::createReader($inputFileType);
	  $objPHPExcel = $objReader->load($pathArray['path']);
	} catch (Exception $e) {
	  die('加载文件发生错误："' . pathinfo($pathArray['path'], PATHINFO_BASENAME) . '": ' . $e->getMessage());
	}
	// 确定要读取的sheet
	$sheet = $objPHPExcel->getSheet(0);
	$highestRow = $sheet->getHighestRow();
	if($end == ''){
	   $highestColumn = $sheet->getHighestColumn();
	}else{
	   $highestColumn = $end;
	}

	$excel_data = array();  $user_array = array();
	  for ($currentRow=$row; $currentRow <= $highestRow; $currentRow++) { 
		for ($currentColumn=$begin; $currentColumn <= $highestColumn ; $currentColumn++) { 
			$getValue = $objPHPExcel->getActiveSheet()->getCell($currentColumn . $currentRow)->getValue();
			$excel_data[$currentColumn]= trim($getValue);
		}            
		  $user_array[] =  $excel_data;//array_values($excel_data);
	 
	  }       
		return $user_array;
}



	
}